unit TimeSorrt;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

const
  r=10000;
  n=10000;
  h=10;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Timer1: TTimer;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    StaticText1: TStaticText;
    StaticText2: TStaticText;
    StaticText3: TStaticText;
    StaticText4: TStaticText;
    StaticText5: TStaticText;
    StaticText6: TStaticText;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  arrInteger = array of integer;

var
  Form1: TForm1;
  arr, arr2:arrInteger;
  x1, x2, x3, x4:integer;

implementation

{$R *.dfm}
{function heapify(num, n:integer; mass:arrInteger): arrInteger;
var a, b,t:integer;
begin
  while (true) do;
  begin
    a:= 20000000;
    b:= 20000000;
    if ((2*num + 1) < n) then
      a:= mass[2*num + 1];
    if ((2*num + 2) < n) then
      b:= mass[2*num + 2];
    if (a <= b) and (a < mass[num]) and (a <> 20000000) then
    begin
      t:= mass[2*num + 1];
      mass[2*num + 1]:= mass[num];
      mass[num]:= t;
      num:= 2 * num + 1;
    end
    else
    if (b <= a) and (b < mass[num]) and (b <> 20000000) then
    begin
      t:= mass[2*num + 2];
      mass[2*num + 2]:= mass[num];
      mass[num]:= t;
      num:= 2*num + 2;
    end
    else
      break;
  end;
    heapify:=mass;
end;}

procedure TForm1.FormCreate(Sender: TObject);
var i,m:integer;
begin
  setlength(arr,n);
  setlength(arr2, n);
  x1:=0;
  x2:=0;
  x3:=0;
  x4:=0;
  Form1.Label1.Top:=h+Form1.Image1.Top;
  Form1.Label1.Height:=h;
  Form1.StaticText1.Top:=Form1.Label1.Top;
  Form1.StaticText4.Top:=Form1.Label1.Top;

  Form1.Label2.Top:=3*h+Form1.Image1.Top;
  Form1.Label2.Height:=h;
  Form1.StaticText2.Top:=Form1.Label2.Top;
  Form1.StaticText5.Top:=Form1.Label2.Top;

  Form1.Label3.Top:=5*h+Form1.Image1.Top;
  Form1.Label3.Height:=h;
  Form1.StaticText3.Top:=Form1.Label3.Top;
  Form1.StaticText6.Top:=Form1.Label3.Top;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var i, j, l, t,m:integer;
    a:real;
    flag:boolean;
begin
  randomize;
  for i:=0 to n-1 do
  begin
    arr2[i]:=random(r);
    arr[i]:=arr2[i];
  end;

  t:=GetTickCount;
  for i:=0 to n-2 do
  begin
    for j:=0 to n-i-2 do
    begin
      if arr[i]>arr[1+i] then
      begin
        l:=arr[i+1];
        arr[i+1]:=arr[i];
        arr[i]:=l;
      end;
    end;
  end;
  x1:=GetTickCount - t+x1;
  for i:=0 to n-1 do
    arr[i]:=arr2[i];

  t:=GetTickCount;
  for i:=0 to n-2 do
  begin
    flag:=false;
    for j:=0 to n-i-2 do
    begin
      if arr[i]>arr[1+i] then
      begin
        l:=arr[i+1];
        arr[i+1]:=arr[i];
        arr[i]:=l;
        flag:=true;
      end;
      if not flag then
        break;
    end;
  end;
  x2:=GetTickCount - t+x2;
  for i:=0 to n-1 do
    arr[i]:=arr2[i];

  t:=GetTickCount;
  for i:=0 to n-1 div 2 do
  begin
    flag:=false;
    for j:= 0 to n-i-2 do
    begin
      if arr[i]>arr[1+i] then
      begin
        l:=arr[i+1];
        arr[i+1]:=arr[i];
        arr[i]:=l;
        flag:=true;
      end;
      if arr[n-i-1]>arr[n-i] then
      begin
        l:=arr[n-i];
        arr[n-i]:=arr[n-i-1];
        arr[n-i-1]:=l;
        flag:=true;
      end;
    end;
    if not flag then
      break;
  end;
  x3:=GetTickCount - t + x3;

  
  {for i:=0 to n-1 do
    arr[i]:=arr2[i];
  t:=GetTickCount;
  begin
    for i :=0 to n - 1 do
    begin
      heapify(i, n, arr);
      begin
      m:= n;
        for i: = 0 to n-1 do
        begin
          t:= arr[m - 1];
          arr[m - 1]:= arr[0];
          arr[0]:= t;
          m:=m-1;
          arr:= heapify(0, m, arr);
        end;
      end;
    end;
  end;
  x4:=GetTickCount - t + x4;}


  if x2<>0 then
    a:=(Form1.Image1.Width-h)/x2
  else
    a:=1;
  Form1.Image1.Canvas.Rectangle(clientRect);
  Form1.Image1.Canvas.Rectangle(h,h, round(h+x1*a), 2*h);
  Form1.Image1.Canvas.Rectangle(h,3*h, round(h+x2*a), 4*h);
  Form1.Image1.Canvas.Rectangle(h,5*h, round(h+x3*a), 6*h);
  //Form1.Image1.Canvas.Rectangle(h,7*h, round(h+x4*a), 8*h);

  Form1.Label1.Caption:=IntToStr(x1);
  Form1.Label2.Caption:=IntToStr(x2);
  Form1.Label3.Caption:=IntToStr(x3);
end;

end.






